/**
 * 去前后空格
 * @author tom
 */
String.prototype.Trim = function () {
  return this.replace(/(^\s*)|(\s*$)/g, "");
};

/**
 * 去除所有空格
 * @returns
 */
String.prototype.AllTrim = function () {
	  return this.replace(/\s/g,"");
};

/**
 *开始日期与结束日期比较
 *startDate晚于endDate 返回false
 */
dateCompare = function (startDate, endDate) {
  var arr = startDate.split("-");
  var starttime = new Date(arr[0], arr[1], arr[2]);
  var starttimes = starttime.getTime();
  var arrs = endDate.split("-");
  var lktime = new Date(arrs[0], arrs[1], arrs[2]);
  var lktimes = lktime.getTime();
  if (starttimes >= lktimes) {
    return false;
  } else {
    return true;
  }
};
/**
 * 加载学年
 * @param showNum 需要显示年数
 */
createSchYearCombo = function (showNum) {
  var ss = [];
  var day = new Date();
  var year = day.getFullYear();
  // TODO:添加了请选择。@author liu.wd;
  ss.push({optVal: "", optText: $.fn.combobox.defaults.emptyText});
  if (showNum) {
    var num = Math.floor(showNum / 2);
    var startYear = year - num;
    for (var i = 0; i < showNum; i++) {
      var val = {optVal: startYear + i, optText: startYear + i};
      ss.push(val);
    }
  }
  return ss;
};

/**
 * 加载时段
 * @param showNum
 */
createTimeRange = function () {
  var ss = [];
  for (var i = 0; i <= 24; i++) {
    var val = null;
    if (i < 10) {
      val = {optVal: "0" + i.toString() + "00", optText: "0" + i.toString() + ":00"};
    } else {
      val = {optVal: i.toString() + "00", optText: i.toString() + ":00"};
    }
    ss.push(val);
  }
  return ss;
};

/**
 *查询当前日期
 *返回String
 */
getNowFormatDate = function () {
  var day = new Date();
  var Year = 0;
  var Month = 0;
  var Day = 0;
  var CurrentDate = "";
  //初始化时间
  //Year= day.getYear();//有火狐下2008年显示108的bug
  Year = day.getFullYear();//ie火狐下都可以
  Month = day.getMonth() + 1;
  Day = day.getDate();
  //Hour = day.getHours();
  // Minute = day.getMinutes();
  // Second = day.getSeconds();
  CurrentDate += Year + "-";
  if (Month >= 10) {
    CurrentDate += Month + "-";
  } else {
    CurrentDate += "0" + Month + "-";
  }
  if (Day >= 10) {
    CurrentDate += Day;
  } else {
    CurrentDate += "0" + Day;
  }
  return CurrentDate;
};

/**
 * 格式化TimeStamp类型时间串
 * 返回格式为“2013-12-25 09:53:22”
 */
formatTimeStampDate = function (date) {
  var datetime = new Date(date);
  var year = datetime.getFullYear();
  var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
  var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
  var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
  var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
  var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
  return year + "-" + month + "-" + date + "   " + hour + ":" + minute + ":" + second;
};

/**
 * 格式化TimeStamp类型为日期
 * 返回格式为“2013-12-25 09:53:22”
 * @param date
 * @returns Date
 */
formatStampToDate = function (date) {
	  var datetime = new Date(date);
	  var year = datetime.getFullYear();
	  var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
	  var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
	  var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
	  var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
	  var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
	  return new Date(year, month, date, hour, minute, second);
	};

/**
 * 格式化时间
 * @param date
 * @returns 格式为“2013-12-25”
 */
formatDate = function (date) {
  var datetime = new Date(date);
  var year = datetime.getFullYear();
  var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
  var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
  return year + "-" + month + "-" + date;
};

/**
 * 格式化时间
 * @param date
 * @returns 格式为“2013-12-25  06:06”
 */
formatDate1 = function (date) {
  var datetime = new Date(date);
  var year = datetime.getFullYear();
  var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
  var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
  var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
  var minute = datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes();
  return year + "-" + month + "-" + date + "&nbsp&nbsp" + hour + ":" + minute;
};
/**
 * 设置等比宽度
 * @param percent
 * @param num
 * @returns {Number}
 */
getWidth = function (percent, num) {
  //var leftdivwidth = $('.ui-layout-west', window.parent.document).width();
  var allwidth = $(window).width() - 200;
  if (num == null) {
    return allwidth * percent - 2;
  } else {
    return allwidth / num - 2;
  }
}

/**
 *将form表单元素的值序列化成JSON对象
 *form 表单名字
 *(注意如果是保存和修改的时候请注意,如果Form中有属性值为null会转成"",
 *所以如果有ID为NULL也会传一个空字符串,后台如果用saveorupdate方法的话会执行update方法)
 */
serializeObject = function (form) {
  var v = {};
  var o = $("#" + form).serializeArray();
  for (var i in o) {
    if (typeof (v[o[i].name]) == "undefined") {
      v[o[i].name] = o[i].value.Trim();
    } else {
      v[o[i].name] += "," + o[i].value.Trim();
    }
  }
  return v;
};
/**
 * 根据编码的code值和data数组，展示文件值。
 */
function showTextByVal(val, record) {
  if (val != 0)
    if (!val)
      return "";
  if (val === "" || val == null || val == undefined)
    return "";
  var data = record.data;
  if (data instanceof Array) {
    var realVal = "";
    for (var p in data) {
      if (data[p][record.valueField] === "")
        continue;
      if (data[p][record.valueField] == val) {
        realVal = data[p][record.textField];
        return realVal || "";
      }
    }
    return realVal || val;
  }
  return val || "";
}
/**
 * 根据编码的code值和data数组，展示文件值。
 */
function showTextByVal_(val, record, data_) {
  if (!val)
    return "";
  var data = data_;
  if (data instanceof Array) {
    var realVal = "";
    for (var p in data) {
      if (data[p][record.valueField] == val) {
        realVal = data[p][record.textField];
        return realVal || "";
      }
    }
    return realVal || val;
  }
  return val || "";
}
/**
 * 根据固定日历数据去的是星期几。
 * @param dateStr  日历格式 yyyy-MM-dd
 * @returns 返回值是 0,1,2,3,4,5,6。
 */
function getWeekDay(dateStr) {
  var dataObj = new Date();
  var dsArray = dateStr.split("-");
  dataObj.setFullYear(dsArray[0]);
  dataObj.setMonth(dsArray[1] - 1);
  dataObj.setDate(dsArray[2]);
  return dataObj.getDay();
}

/**
 * 根据固定日历数据去的是星期几。
 * @param dateStr  日历格式 yyyy-MM-dd
 * @returns 返回值："星期几"。
 */
function getWeekChar(dateStr) {
  var day = getWeekDay(dateStr);
  var _wc = "";
  switch (day) {
    case 0:
      _wc = "星期日";
      break;
    case 1:
      _wc = "星期日";
      break;
    case 2:
      _wc = "星期日";
      break;
    case 3:
      _wc = "星期日";
      break;
    case 4:
      _wc = "星期日";
      break;
    case 5:
      _wc = "星期日";
      break;
    case 6:
      _wc = "星期日";
      break;
  }
  return _wc;
}

/**
 * HTML打印
 * @param printDiv
 * @param isclose
 */
function printView(printDiv, isclose) {
    var  bdhtml = window.document.body.innerHTML;//获取当前页的html代码
    
/*    var sprnstr = '<span id="startprint'+oper+'"></span>';// "<!--startprint" + oper + "-->";//设置打印开始区域
    var eprnstr =  '<span id="endprint'+oper+'"></span>'; //"<!--endprint" + oper + "-->";//设置打印结束区域
    var prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + sprnstr.length); //从开始代码向后取html
    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));//从结束代码向前取html*/
    
    var printHTML = window.document.getElementById(printDiv).innerHTML;
    window.document.body.innerHTML = printHTML;
    window.print();
    window.document.body.innerHTML = bdhtml;
    if (!isclose)
    	window.close();
}